package com.rambo.weather.provider;

import com.rambo.baselib.model.weather.DayWeather;
import com.rambo.baselib.model.weather.WarnWeather;
import com.rambo.baselib.model.weather.Weather;
import com.rambo.baselib.model.weather.WeatherApiResult;
import com.rambo.baselib.provider.BaseViewHolder;
import com.rambo.baselib.provider.ListItemProvider;
import com.rambo.baselib.util.DateUtil;
import com.rambo.weather.ResourceTable;
import com.rambo.weather.bean.WeatherBean;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.app.Context;

import java.util.List;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/18 16:23
 **********************************************************************/
public class WeatherProvider extends ListItemProvider<WeatherBean, BaseViewHolder> {
    public WeatherProvider(Context context) {
        super(context, ResourceTable.Layout_weather_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, WeatherBean item, int position) {

        Weather nowWeather = item.nowResult != null ? item.nowResult.getNow() : null;
        List<WarnWeather> warnWeathers = item.warnResult != null ? item.warnResult.getWarning() : null;
        List<DayWeather> dayWeathers = item.dayResult != null ? item.dayResult.getDaily() : null;
        helper.setText(ResourceTable.Id_tv_city, item.city)
                .setText(ResourceTable.Id_tv_now_temp, nowWeather != null ? nowWeather.getTemp() + "" : "")
                .setText(ResourceTable.Id_tv_now_text, nowWeather != null ? nowWeather.getText() + "" : "")
                .setText(ResourceTable.Id_tv_now, nowWeather != null ? DateUtil.formatMmDdHhmm(nowWeather.getObsTime()) : "")


                .setText(ResourceTable.Id_tv_feelsLike, nowWeather != null ? nowWeather.getFeelsLike() + "℃" : "")
                .setText(ResourceTable.Id_tv_humidity, nowWeather != null ? nowWeather.getHumidity() + "%" : "")
                .setText(ResourceTable.Id_tv_windScale, nowWeather != null ? nowWeather.getWindScale() + "级" : "")
                .setText(ResourceTable.Id_tv_pressure, nowWeather != null ? nowWeather.getPressure() + "hpa" : "");

        if (warnWeathers == null || warnWeathers.size() == 0) {
            helper.setVisibility(ResourceTable.Id_tv_warn, Component.INVISIBLE);
        } else {
            WarnWeather warnWeather = warnWeathers.get(0);
            helper.setVisibility(ResourceTable.Id_tv_warn, Component.INVISIBLE)
                    .setText(ResourceTable.Id_tv_warn, warnWeather.getText() + warnWeather.getLevel() + "预警");
        }
        if (dayWeathers != null) {
            for (int i = 0; i < dayWeathers.size(); i++) {
                DayWeather dayWeather = dayWeathers.get(0);
                if (i == 0) {
                    helper.setText(ResourceTable.Id_tv_day0_text, dayWeather.getTextNight())
                            .setText(ResourceTable.Id_tv_day0_temp, dayWeather.getTempMin() + "℃~" + dayWeather.getTempMax()+"℃");
                    continue;
                }
                if (i == 1) {
                    helper.setText(ResourceTable.Id_tv_day1_text, dayWeather.getTextNight())
                            .setText(ResourceTable.Id_tv_day1_temp, dayWeather.getTempMin() + "℃~" + dayWeather.getTempMax()+"℃");
                    continue;
                }
                if (i == 2) {
                    helper.setText(ResourceTable.Id_tv_day2_text, dayWeather.getTextNight())
                            .setText(ResourceTable.Id_tv_day2_temp, dayWeather.getTempMin() + "℃~" + dayWeather.getTempMax()+"℃");
                    break;
                }
            }
        }
    }
}
